package demo1;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectClassDemo {
    // 创建对象
    public static void reflectNewInstance() {
        Class<?> classStudent = null;
        try {
            classStudent = Class.forName("demo1.Student");
            Student student = (Student) classStudent.newInstance();

            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        Class<?> classStudent = null;
        try {
          classStudent = Class.forName("demo1.Student");
          //获得构造方法
            Constructor<?> constructor = classStudent.getDeclaredConstructor(String.class, int.class);
            //私有的属性name要你确认调用才能调用
            constructor.setAccessible(true);

            Student student = (Student) constructor.newInstance("xiaoming", 15);

            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有属性
    public static void reflectPrivateField() {
        Class<?> classStudent = null;

        try{
            classStudent = Class.forName("demo1.Student");
            Field field = classStudent.getDeclaredField("name");
            field.setAccessible(true);
            Student student = (Student) classStudent.newInstance();
            field.set(student, "caocao");
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }
    // 反射私有方法
    public static void reflectPrivateMethod() {
       try {
           Class<?> classStudent = Class.forName("demo1.Student");
           Method method = classStudent.getDeclaredMethod("function", String.class);
           method.setAccessible(true);
           Student student = (Student) classStudent.newInstance();

           method.invoke(student, "我是一个反射的参数");

       } catch(ClassNotFoundException e) {
           e.printStackTrace();
       } catch (NoSuchMethodException e) {
           throw new RuntimeException(e);
       } catch (InstantiationException e) {
           throw new RuntimeException(e);
       } catch (IllegalAccessException e) {
           throw new RuntimeException(e);
       } catch (InvocationTargetException e) {
           throw new RuntimeException(e);
       }

    }

    public static void main(String[] args) {
        //reflectNewInstance();
        //reflectPrivateConstructor();
        //reflectPrivateField();
        reflectPrivateMethod();

    }
}